
package com.sausageking.client.mvp;

import com.google.common.base.Strings;
import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.inject.Inject;
import com.sausageking.client.common.ClientUtil;
import com.sausageking.client.common.Context;
import com.sausageking.shared.Constants;

public abstract class BaseActivity extends AbstractActivity {

  private String token;

  private ActivityPlace<? extends BaseActivity> place;

  DialogBox loadingDialogBox;

  SimplePanel glass;

  @Inject
  protected ClientUtil urlUtil;

  @Override
  public void start(AcceptsOneWidget container, EventBus eventBus) {
    startActivity(container, eventBus);
  }

  public void setToken(String token) {
    this.token = token;
  }

  public String getToken() {
    return token;
  }

  public ActivityPlace<? extends BaseActivity> getPlace() {
    return place;
  }

  public void setPlace(ActivityPlace<? extends BaseActivity> place) {
    this.place = place;
  }

  // redirect the browser to the given url
  public static native void redirect(String url)/*-{
		$wnd.location = url;
  }-*/;

  public void processing() {
    // // if (glass == null) {
    // // glass = new SimplePanel();
    // // glass.setStyleName("popupglass");
    // // glass.setHeight(Window.getClientHeight() + "px");
    // // Window.addResizeHandler(new ResizeHandler() {
    // // public void onResize(ResizeEvent event) {
    // // glass.setHeight(event.getHeight() + "px");
    // // }
    // // });
    // // RootPanel.get().add(glass);
    // }
    //
    // glass.setVisible(true);
    if (loadingDialogBox == null) {
      loadingDialogBox = new DialogBox();
      loadingDialogBox.setStyleName("loadingindicator");
      loadingDialogBox.setSize("90px", "90px");
      Image loadingIndicator = new Image();
      loadingIndicator.setUrl("resources/loadingindicator.gif");
      loadingDialogBox.add(loadingIndicator);
    }
    loadingDialogBox.show();
    loadingDialogBox.setGlassEnabled(true);
    loadingDialogBox.center();

  }

  public void processed() {
    loadingDialogBox.setGlassEnabled(false);
    loadingDialogBox.hide();
    // glass.setVisible(false);
  }

  abstract protected void startActivity(AcceptsOneWidget container,
      EventBus eventBus);

  // Extract context from cookie and hashtag token
  public Context getContext() {
    String keepLastNCheckins = urlUtil.getParameterFromToken(
        Constants.KEEP_LAST_N_CHECKINS, token);
    String keepLastNRedemptions = urlUtil.getParameterFromToken(
        Constants.KEEP_LAST_N_REDEMPTIONS, token);
    int keepC = Strings.isNullOrEmpty(keepLastNCheckins)
        ? Integer.MAX_VALUE
        : Integer.valueOf(keepLastNCheckins);
    int keepR = Strings.isNullOrEmpty(keepLastNRedemptions)
        ? Integer.MAX_VALUE
        : Integer.valueOf(keepLastNRedemptions);

    Context context = new Context()
        .setEmail(Cookies.getCookie(Constants.EMAIL))
        .setSessionId(Cookies.getCookie(Constants.SESSION_ID))
        .setMerchantId(urlUtil.getParameterFromToken(Constants.MERCHANT, token))
        .setKeepLastNCheckins(keepC).setKeepLastNRedemptions(keepR);
    return context;
  }
}
